package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.InventoryAudits;
import com.example.mall.dto.InventoryAuditsDto;
import com.example.mall.mapper.InventoryAuditsMapper;
import com.example.mall.query.InventoryAuditsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IInventoryAuditsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存盘点表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class InventoryAuditsServiceImpl extends ServiceImpl<InventoryAuditsMapper, InventoryAudits> implements IInventoryAuditsService {

    @Autowired
    private InventoryAuditsMapper inventoryAuditsMapper;

    @Override
    public PageDTO<InventoryAuditsDto> queryPage(InventoryAuditsQuery inventoryAuditsQuery) {
        if (inventoryAuditsQuery == null) {
            inventoryAuditsQuery = new InventoryAuditsQuery();
        }
        Page<InventoryAudits> page = inventoryAuditsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<InventoryAudits> cartPage = lambdaQuery()
                .like(inventoryAuditsQuery.getAuditId()!= null, InventoryAudits::getAuditId, inventoryAuditsQuery.getAuditId())
                .like(inventoryAuditsQuery.getProductId()!= null, InventoryAudits::getProductId, inventoryAuditsQuery.getProductId())
                .like(inventoryAuditsQuery.getSpecId()!= null, InventoryAudits::getSpecId, inventoryAuditsQuery.getSpecId())
                .like(inventoryAuditsQuery.getAuditStock()!= null, InventoryAudits::getAuditStock, inventoryAuditsQuery.getAuditStock())
                .like(inventoryAuditsQuery.getSystemStock()!= null, InventoryAudits::getSystemStock, inventoryAuditsQuery.getSystemStock())
                .like(inventoryAuditsQuery.getAuditStatus()!= null, InventoryAudits::getAuditStatus, inventoryAuditsQuery.getAuditStatus())
                .like(inventoryAuditsQuery.getAuditBy()!= null, InventoryAudits::getAuditBy, inventoryAuditsQuery.getAuditBy())
                .apply(inventoryAuditsQuery.getAuditTime() != null,
                        "DATE_FORMAT(audit_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        inventoryAuditsQuery.getAuditTime())
                .orderByDesc(InventoryAudits::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, InventoryAuditsDto.class);
    }

    @Override
    public InventoryAudits queryInventoryAuditsById(Long auditId) {
        AssertUtil.isNotNull(auditId, "库存盘点ID不能为空");
        return null;
    }

    @Override
    public List<InventoryAudits> queryInventoryAuditsList() {
        return this.list();
    }

    @Override
    public void insert(InventoryAuditsDto inventoryAuditsDto) {
        InventoryAudits inventoryAudits = new InventoryAudits();
        CopyUtils.copyProperties(inventoryAuditsDto, inventoryAudits);
        inventoryAudits.setAuditId(Long.valueOf(CodeGenerateUtils.generateUniqueId()));
        inventoryAudits.setAuditStatus(BaseConstants.EFFECTIVE_STATUS);
        inventoryAudits.setCreateDate(new Date());
        inventoryAudits.setUpdateDate(new Date());
        inventoryAuditsMapper.insert(inventoryAudits);
    }

    @Override
    public void update(InventoryAuditsDto inventoryAuditsDto) {
        AssertUtil.isNotNull(inventoryAuditsDto.getAuditId(), "库存盘点ID不能为空");
        InventoryAudits inventoryAudits = inventoryAuditsMapper.selectById(inventoryAuditsDto.getAuditId());
        AssertUtil.isNotNull(inventoryAudits, "库存盘点记录不存在");
        CopyUtils.copyProperties(inventoryAuditsDto, inventoryAudits);
        inventoryAudits.setUpdateDate(new Date());
        inventoryAuditsMapper.updateById(inventoryAudits);
    }

    @Override
    public void delete(Long auditId) {
        AssertUtil.isNotNull(auditId, "库存盘点ID不能为空");
        InventoryAudits inventoryAudits = inventoryAuditsMapper.selectById(auditId);
        AssertUtil.isNotNull(inventoryAudits, "库存盘点记录不存在");
        inventoryAudits.setAuditStatus(BaseConstants.LOSE_STATUS);
        inventoryAudits.setUpdateDate(new Date());
        inventoryAuditsMapper.updateById(inventoryAudits);
    }

    @Override
    public void deleteBatch(List<Long> auditIds) {
        AssertUtil.isNotNull(auditIds, "库存盘点ID列表不能为空");
        List<InventoryAudits> inventoryAudits = inventoryAuditsMapper.selectBatchIds(auditIds);
        AssertUtil.isNotNull(inventoryAudits, "库存盘点记录不存在");
        for (InventoryAudits inventoryAudit : inventoryAudits) {
            inventoryAudit.setAuditStatus(BaseConstants.LOSE_STATUS);
            inventoryAudit.setUpdateDate(new Date());
            inventoryAuditsMapper.updateById(inventoryAudit);
        }
    }
}
